<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="机构简称" prop="shortName">
        <el-input
          v-model="queryParams.shortName"
          placeholder="请输入机构简称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="机构全称" prop="fullName">
        <el-input
          v-model="queryParams.fullName"
          placeholder="请输入机构全称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="机构logo地址" prop="logo">
        <el-input
          v-model="queryParams.logo"
          placeholder="请输入机构logo地址"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="机构地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入机构地址"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="官方邮箱" prop="officialEmail">
        <el-input
          v-model="queryParams.officialEmail"
          placeholder="请输入官方邮箱"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="法人姓名" prop="lpName">
        <el-input
          v-model="queryParams.lpName"
          placeholder="请输入法人姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="法人身份证号" prop="lpidNo">
        <el-input
          v-model="queryParams.lpidNo"
          placeholder="请输入法人身份证号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="法人身份证人像面图片地址" prop="lpidFrontImg">
        <el-input
          v-model="queryParams.lpidFrontImg"
          placeholder="请输入法人身份证人像面图片地址"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="法人身份证国徽面图片地址" prop="lpidBackImg">
        <el-input
          v-model="queryParams.lpidBackImg"
          placeholder="请输入法人身份证国徽面图片地址"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="营业执照号码" prop="blNumber">
        <el-input
          v-model="queryParams.blNumber"
          placeholder="请输入营业执照号码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="统一社会信用代码" prop="creditCode">
        <el-input
          v-model="queryParams.creditCode"
          placeholder="请输入统一社会信用代码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="营业执照图片地址" prop="blImg">
        <el-input
          v-model="queryParams.blImg"
          placeholder="请输入营业执照图片地址"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付账号" prop="account">
        <el-input
          v-model="queryParams.account"
          placeholder="请输入支付账号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="介绍" prop="introduction">
        <el-input
          v-model="queryParams.introduction"
          placeholder="请输入介绍"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['institution:institution:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['institution:institution:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['institution:institution:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['institution:institution:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="institutionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="机构ID" align="center" prop="id" />
      <el-table-column label="机构简称" align="center" prop="shortName" />
      <el-table-column label="机构全称" align="center" prop="fullName" />
      <el-table-column label="机构logo地址" align="center" prop="logo" />
      <el-table-column label="机构的审核状态" align="center" prop="status" />
      <el-table-column label="机构地址" align="center" prop="address" />
      <el-table-column label="官方邮箱" align="center" prop="officialEmail" />
      <el-table-column label="法人姓名" align="center" prop="lpName" />
      <el-table-column label="法人身份证号" align="center" prop="lpidNo" />
      <el-table-column label="法人身份证人像面图片地址" align="center" prop="lpidFrontImg" />
      <el-table-column label="法人身份证国徽面图片地址" align="center" prop="lpidBackImg" />
      <el-table-column label="营业执照号码" align="center" prop="blNumber" />
      <el-table-column label="统一社会信用代码" align="center" prop="creditCode" />
      <el-table-column label="营业执照图片地址" align="center" prop="blImg" />
      <el-table-column label="支付账号" align="center" prop="account" />
      <el-table-column label="介绍" align="center" prop="introduction" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['institution:institution:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['institution:institution:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改机构对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="institutionRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="机构简称" prop="shortName">
          <el-input v-model="form.shortName" placeholder="请输入机构简称" />
        </el-form-item>
        <el-form-item label="机构全称" prop="fullName">
          <el-input v-model="form.fullName" placeholder="请输入机构全称" />
        </el-form-item>
        <el-form-item label="机构logo地址" prop="logo">
          <el-input v-model="form.logo" placeholder="请输入机构logo地址" />
        </el-form-item>
        <el-form-item label="机构地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入机构地址" />
        </el-form-item>
        <el-form-item label="官方邮箱" prop="officialEmail">
          <el-input v-model="form.officialEmail" placeholder="请输入官方邮箱" />
        </el-form-item>
        <el-form-item label="法人姓名" prop="lpName">
          <el-input v-model="form.lpName" placeholder="请输入法人姓名" />
        </el-form-item>
        <el-form-item label="法人身份证号" prop="lpidNo">
          <el-input v-model="form.lpidNo" placeholder="请输入法人身份证号" />
        </el-form-item>
        <el-form-item label="法人身份证人像面图片地址" prop="lpidFrontImg">
          <el-input v-model="form.lpidFrontImg" placeholder="请输入法人身份证人像面图片地址" />
        </el-form-item>
        <el-form-item label="法人身份证国徽面图片地址" prop="lpidBackImg">
          <el-input v-model="form.lpidBackImg" placeholder="请输入法人身份证国徽面图片地址" />
        </el-form-item>
        <el-form-item label="营业执照号码" prop="blNumber">
          <el-input v-model="form.blNumber" placeholder="请输入营业执照号码" />
        </el-form-item>
        <el-form-item label="统一社会信用代码" prop="creditCode">
          <el-input v-model="form.creditCode" placeholder="请输入统一社会信用代码" />
        </el-form-item>
        <el-form-item label="营业执照图片地址" prop="blImg">
          <el-input v-model="form.blImg" placeholder="请输入营业执照图片地址" />
        </el-form-item>
        <el-form-item label="支付账号" prop="account">
          <el-input v-model="form.account" placeholder="请输入支付账号" />
        </el-form-item>
        <el-form-item label="介绍" prop="introduction">
          <el-input v-model="form.introduction" placeholder="请输入介绍" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Institution">
import { listInstitution, getInstitution, delInstitution, addInstitution, updateInstitution } from "@/api/institution/institution";

const { proxy } = getCurrentInstance();

const institutionList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    shortName: null,
    fullName: null,
    logo: null,
    status: null,
    address: null,
    officialEmail: null,
    lpName: null,
    lpidNo: null,
    lpidFrontImg: null,
    lpidBackImg: null,
    blNumber: null,
    creditCode: null,
    blImg: null,
    account: null,
    introduction: null,
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询机构列表 */
function getList() {
  loading.value = true;
  listInstitution(queryParams.value).then(response => {
    institutionList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    shortName: null,
    fullName: null,
    logo: null,
    status: null,
    address: null,
    officialEmail: null,
    lpName: null,
    lpidNo: null,
    lpidFrontImg: null,
    lpidBackImg: null,
    blNumber: null,
    creditCode: null,
    blImg: null,
    account: null,
    introduction: null,
    createTime: null,
    updateTime: null
  };
  proxy.resetForm("institutionRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加机构";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getInstitution(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改机构";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["institutionRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateInstitution(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addInstitution(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除机构编号为"' + _ids + '"的数据项？').then(function() {
    return delInstitution(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('institution/institution/export', {
    ...queryParams.value
  }, `institution_${new Date().getTime()}.xlsx`)
}

getList();
</script>
